from ChessBoard import *

"""
Responsible for storing the moves made during a game of chess. 
Note: I looked at other groups work and chose to add getLastMove module
to this class here. It was a simple function in a separate file - now it is part of an OO class.

I have also added getLastTextMove here from ChessBoard - it made sense for my end to end test. 
"""
class MoveStore:

    def __init__(self, chessBoard):
        self.cb = chessBoard

    def getAllTextMoves(self, format=1):
        if self.cb._state_stack_pointer<=1:
            return None

        result = []
        point = self.cb._state_stack_pointer
        self.cb.gotoFirst()

        while True:
            move = self.cb._moves[self.cb._state_stack_pointer-1]
            result.append(self.cb._formatTextMove(move,format))
            if self.cb._state_stack_pointer >= len(self.cb._state_stack)-1:
                break
            self.cb.redo()

        self.cb._state_stack_pointer = point
        self.cb.loadCurState()

        return result

    def getLastMove(self):
        if self.cb._state_stack_pointer<=1:
            return None
        
        self.cb.undo()
        move = self.cb._moves[self.cb._state_stack_pointer-1]
        result = (move[1],move[2])
        self.cb.redo()
        return result
        
    def getLastTextMove(self, format=1):
        if self.cb._state_stack_pointer<=1:
            return None
        
        self.cb.undo()
        move = self.cb._moves[self.cb._state_stack_pointer-1]
        result = self.cb._formatTextMove(move, format)
        self.cb.redo()
        return result




        

